// pages/law/clist/index.js
import util from '../../../utils/util';

var startX, startY, key, endX, endY, maxRight=60;

Page({
  data:{
    userInfo: {}
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    // this.getUserCollection();
  },
  getUserCollection: function(){
    //调用应用实例的方法获取全局数据
    var that = this;
    var _appInstance = getApp();
    var _globalData = _appInstance.globalData.userInfo;
    this.setData({
      userInfo: _globalData
    });
    wx.setNavigationBarTitle({
      title: '我的收藏',
      success: function(res) {
        // success
      }
    });

    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    });

    // 获取列表
    wx.request({
      url: `${getApp().globalData.serverHost}/wx/get/law/collection`, //仅为示例，并非真实的接口地址
      data: {uid: _globalData.unionId},
      method: "post",
      header: { 'content-type': 'application/json'},
      success: function(res) {
        wx.hideToast();
        for(var i=0; i<res.data.length; i++){
          res.data[i].collectionTime = util.formatCommonTime(res.data[i].collectionTime);
          res.data[i].right = 0;
          res.data[i].startRight = 0;
        }
        that.setData({
          list: res.data
        });
        wx.stopPullDownRefresh();
      },
      fail: function(res){
        console.log(res);
        wx.stopPullDownRefresh();
      }
    });
  },
  onPullDownRefresh: function(){
    this.getUserCollection();
  },
  drawStart : function(e){  
     // console.log("drawStart");  
    var touch = e.touches[0];  
    startX = touch.clientX;  
    startY = touch.clientY;  
    var list = this.data.list;  
    for(var i in list){  
        var data = list[i];  
        data.startRight = data.right = 0;  
    }  
    this.setData({  
        list:list  
    });  
    key = true;  
  },  
  drawEnd : function(e){  
    console.log("drawEnd");  
    var list = this.data.list;  
    for(var i in list){  
        var data = list[i];  
        if(data.right < maxRight){  
            data.right = 0;  
        }else{  
            data.right = maxRight;  
        }  
    }  
    this.setData({  
        list:list  
    });  
  },  
  drawMove : function(e){  
      //console.log("drawMove");  
    var self = this;  
    var dataId = e.currentTarget.id;  
    var list = this.data.list;  
    if(key){  
        var touch = e.touches[0];  
        endX = touch.clientX;  
        endY = touch.clientY;  
        console.log("startX="+startX+" endX="+endX );  
        if(endX - startX == 0)  
           return ;  
        var res = list;  
           //从右往左  
  
            if((endX - startX) < 0){  
                for(var k in res){  
                    var data = res[k];  
                    if(res[k].id == dataId){  
                        var startRight = res[k].startRight;  
                        var change = startX - endX;  
                        startRight += change;  
                        if(startRight > maxRight)  
                            startRight = maxRight;  
                        res[k].right = startRight;  
                    }  
                }  
            }else{//从左往右  
                for(var k in res){  
                    var data = res[k];  
                    if(res[k].id == dataId){  
                        var startRight = res[k].startRight;  
                        var change = endX - startX;  
                        startRight -= change;  
                        if(startRight < 0)  
                            startRight = 0;  
                        res[k].right = startRight ;  
                    }  
                }  
            }  
            self.setData({  
                list:list  
            });  
                      
    }  
  },  
  //删除item  
  delItem: function(e){  
    var _dataId = e.target.dataset.id; 

    var that = this;
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    });

    // 获取列表
    wx.request({
      url: `${getApp().globalData.serverHost}/wx/delete/law/collection`, //仅为示例，并非真实的接口地址
      data: {
        uid: this.data.userInfo.unionId,
        lid: _dataId
      },
      method: "post",
      header: {
          'content-type': 'application/json'
      },
      success: function(res) {
        wx.hideToast();
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 1000
        });
      },
      fail: function(res){
        console.log(res);
      }
    })

    var list = this.data.list;  
    var newCardTeams = [];   
    for(var i in list){  
        var item = list[i];  
        if(item.id != _dataId){  
          newCardTeams.push(item);  
        }  
    }  
    this.setData({  
        list:newCardTeams  
     });  
  }, 
  hideOpts: function(){
    for(var i=0; i<this.data.list.length; i++){
      this.data.list[i].right = 0;
      this.data.list[i].startRight = 0;
    }
    this.setData({
      list: this.data.list
    });
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
    this.getUserCollection();
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})